package com.maigao.entity.jpa;

import lombok.Data;
import lombok.ToString;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;

import javax.persistence.*;
import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

@Data
@Entity
@Table(name = "tb_class_record")
@DynamicInsert
@DynamicUpdate
public class ClassRecord implements Serializable {

    public enum Status {
        CREATED(0), FINISHED(1), ABANDON(-1);

        Status(int value) {
            this.value = value;
        }

        private int value;

        public int getValue() {
            return value;
        }

        public static Status getByValue(int value) {
            Optional<Status> optional = Arrays.stream(values()).filter(p -> Objects.equals(p.getValue(), value)).findFirst();
            if (optional.isPresent()) {
                return optional.get();
            }
            return null;
        }
    }

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @Column(name = "student_id")
    private Integer studentId;

    @Column(name = "student_name")
    private String studentName;

    @Column(name = "coach_id")
    private Integer coachId;

    @Column(name = "coach_name")
    private String coachName;

    @Column(name = "student_course_id")
    private Integer studentCourseId;

    @Column(name = "course_type")
    private String courseType;

    @Column(name = "schedule_id")
    private Integer scheduleId;

    @Column(name = "class_hour")
    private Integer classHour;

    @Column(name = "schedule_name")
    private String scheduleName;

    @Column(name = "note")
    private String note;

    @Column(name = "course_price")
    private BigDecimal coursePrice;

    @Column(name = "class_from_time")
    private LocalDateTime classFromTime;

    @Column(name = "class_end_time")
    private LocalDateTime classEndTime;

    @Column(name = "status")
    private Integer status;

    @Column(name = "create_time")
    private LocalDateTime createTime;

    @ToString.Exclude   //解决查询时无限递归问题
    @OneToMany(mappedBy = "classRecord", cascade = CascadeType.ALL)
    private List<QueryTakeOffRecord> takeOffRecords;
}
